package agents;

import utils.AiSearch;
import utils.Strategy;
import utils.AiSearch.AiNode;
import env.City;
import env.Move;
import env.World;

public class Search implements Agent {
	private World world;
	private World copiedWorld;
	private City goal;
	private int score;
	private boolean done;
	private Strategy strategy;
	private AiNode node;
	private AiSearch aiSearch;
	public int expands;
	private int cutOff;
	

	

	public Search(World world, World copiedWorld, City init, City goal, Strategy strategy, int cutOff) {
		this.world = world;
		this.copiedWorld = copiedWorld;
		this.goal = goal;
		this.score = 0;
		this.done = false;
		this.strategy = strategy;
		this.aiSearch = new AiSearch(this, world, copiedWorld, copiedWorld.getCityByName(init.getName()), copiedWorld.getCityByName(goal.getName()), strategy, cutOff);
		this.node = aiSearch.search();
		this.expands = aiSearch.getExpands();
		this.cutOff= cutOff;
	}

	@Override
	public Move calcMove() {
		if (cutOff!=0){		//TODO matan check that please	
			copiedWorld = new World(world);
			aiSearch = new AiSearch(this, world, copiedWorld, copiedWorld.getCityByName(world.getCityOfAgent(this).getName()), copiedWorld.getCityByName(goal.getName()), strategy, cutOff);
			node = aiSearch.search();
			expands += aiSearch.getExpands();
		}
		Move move = node.getMove();		
		return move;
	}

	@Override
	public boolean isDone() {
		if(done)
			return done;
		else
		{
			done = !world.chemsOutsideCity(goal);
			return done;
		}
	}

	@Override
	public City getGoal() {		
		return goal;
	}

	@Override
	public int getExpands() {
		return expands;
	}
}
